import testTree from "https://cdn.jsdelivr.net/npm/jest-test-tree@1/dist/jest-test-tree.browser.js";
import HTMLParser from "../../src/HTMLParser/HTMLParser.js";
const {
    core: { describe, it, expect, run },
    enzyme: { mount },
    prettify,
} = window.jestLite;
//! HTML Parser 的测试代码
export default async ({ shop }) => {
    const model = document;
    const op = new HTMLParser();
    await testTree({
        基础测试: [
            [
                "DOM 解构测试",
                () =>
                    op.parse(model, {
                        "h1@result": ["innerText"],
                    }),
                (res) => expect(res.result).toEqual(shop.map((i) => i.name)),
            ],
            [
                "嵌套解构测试",
                () =>
                    op.parse(model, {
                        ".name1": {
                            ".name2@name2": {
                                ".name3": {
                                    ".name4@name4": "innerText",
                                },
                            },
                        },
                    }),
                (res) => expect(res.name2.name4).toBe("这是第三层"),
            ],
            [
                "数组解构测试",
                () => {
                    return op.parse(model, {
                        ".headers": {
                            "h1@result": [
                                {
                                    "innerText@": "",
                                    "id@": "",
                                },
                            ],
                        },
                    });
                },
                (res) =>
                    expect(res.result).toEqual(
                        shop.map((i, index) => ({
                            innerText: i.name,
                            id: "test-" + index,
                        }))
                    ),
            ],
        ],
        DivideType测试: [
            [
                "String",
                () =>
                    op.parse(model, {
                        ".name1": { ".name2@name": "innerText" },
                    }),
                (res) => expect(res.name).toBe("这是第三层"),
            ],
            [
                "Function",
                () =>
                    op.parse(model, {
                        "h1@name": [
                            (data) => {
                                return data.innerText;
                            },
                        ],
                    }),
                (res) => expect(res.name).toEqual(shop.map((i) => i.name)),
            ],
            [
                "Null",
                () =>
                    op.parse(model, {
                        "h1@": null,
                    }),
                (res) => expect(res.h1).toEqual(model.querySelector("h1")),
            ],
            [
                "undefined",
                () =>
                    op.parse(model, {
                        "h1@": undefined,
                    }),
                (res) => expect(res.h1).toEqual(model.querySelector("h1")),
            ],
            // HTML Parser 中不建议使用 Number 作为参数
        ],
    });
    prettify.toHTML(run(), document.body);
};
